package com.strange.mdl.module.mdl.service.activity;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.strange.mdl.framework.mybatis.core.query.LambdaQueryWrapperX;
import com.strange.mdl.module.mdl.controller.app.activity.vo.AppActivityUserSaveReqVO;
import com.strange.mdl.module.mdl.dal.dataobject.activity.ActivityDO;
import com.strange.mdl.module.mdl.dal.dataobject.activity.ActivityUserDO;
import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import com.strange.mdl.module.mdl.controller.admin.activity.vo.*;
import com.strange.mdl.framework.common.pojo.PageResult;
import com.strange.mdl.framework.common.pojo.PageParam;
import com.strange.mdl.framework.common.util.object.BeanUtils;

import com.strange.mdl.module.mdl.dal.mysql.activity.ActivityUserMapper;

import static com.strange.mdl.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.strange.mdl.framework.common.util.collection.CollectionUtils.convertList;
import static com.strange.mdl.framework.common.util.collection.CollectionUtils.diffList;
import static com.strange.mdl.module.mdl.enums.ErrorCodeConstants.*;

/**
 * 活动用户参与记录 Service 实现类
 *
 * @author 密谛灵总控
 */
@Service
@Validated
public class ActivityUserServiceImpl implements ActivityUserService {

    @Resource
    private ActivityUserMapper activityUserMapper;

    @Override
    public void createActivityUser(ActivityUserSaveReqVO createReqVO) {
        // 插入
        ActivityUserDO activityUser = BeanUtils.toBean(createReqVO, ActivityUserDO.class);
        activityUserMapper.insert(activityUser);
    }
    @Override
    public void createAppActivityUser(AppActivityUserSaveReqVO createReqVO) {
        ActivityUserDO activityUserDO = activityUserMapper.selectOne("activity_id", createReqVO.getActivityId(), "user_id", createReqVO.getUserId());
        ActivityUserDO activityUser = BeanUtils.toBean(createReqVO, ActivityUserDO.class);
        if (activityUserDO == null) {
            activityUserMapper.insert(activityUser);
        } else {
            activityUserMapper.update(activityUser, new LambdaQueryWrapper<ActivityUserDO>().eq(ActivityUserDO::getActivityId, activityUserDO.getActivityId()).eq(ActivityUserDO::getUserId, activityUserDO.getUserId()));
        }
    }



    @Override
    public void updateActivityUser(ActivityUserSaveReqVO updateReqVO) {
        // 校验存在
        validateActivityUserExists(updateReqVO.getActivityId());
        validateActivityUserExists(updateReqVO.getUserId());
        // 更新
        ActivityUserDO updateObj = BeanUtils.toBean(updateReqVO, ActivityUserDO.class);
        activityUserMapper.updateById(updateObj);
    }

    @Override
    public void deleteActivityUser(Long id) {
        // 校验存在
        validateActivityUserExists(id);
        // 删除
        activityUserMapper.deleteById(id);
    }

    @Override
        public void deleteActivityUserListByIds(List<Long> ids) {
        // 校验存在
        validateActivityUserExists(ids);
        // 删除
        activityUserMapper.deleteByIds(ids);
        }

    private void validateActivityUserExists(List<Long> ids) {
        List<ActivityUserDO> list = activityUserMapper.selectByIds(ids);
        if (CollUtil.isEmpty(list) || list.size() != ids.size()) {
            throw exception(ACTIVITY_USER_NOT_EXISTS);
        }
    }

    private void validateActivityUserExists(Long id) {
        if (activityUserMapper.selectById(id) == null) {
            throw exception(ACTIVITY_USER_NOT_EXISTS);
        }
    }

    @Override
    public ActivityUserDO getActivityUser(Long id) {
        return activityUserMapper.selectById(id);
    }

    @Override
    public PageResult<ActivityUserDO> getActivityUserPage(ActivityUserPageReqVO pageReqVO) {
        return activityUserMapper.selectPage(pageReqVO);
    }

}